package com.example.flower_shop.dto.review;

import com.example.flower_shop.entity.Review;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ReviewDTO {
    private Long id;
    private Long userId;
    private String username;
    private String userAvatar;
    private Long productId;

    @NotBlank(message = "评论内容不能为空")
    private String content;

    @NotNull(message = "评分不能为空")
    @Min(value = 1, message = "评分最小为1")
    @Max(value = 5, message = "评分最大为5")
    private Integer rating;

    @Builder.Default
    private List<String> imageUrls = new ArrayList<>();

    @Builder.Default
    private List<ReviewReplyDTO> replies = new ArrayList<>();

    @Builder.Default
    private Integer likesCount = 0;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Builder.Default
    private Boolean isLiked = false;

    // 订单编号，用于验证用户是否购买过商品
    private String orderNo;

    // 评论状态字段 - 1表示显示，0表示隐藏
    private Integer status;

    public ReviewDTO(Review review) {
        this.id = review.getId();
        this.userId = review.getUser().getId();
        this.username = review.getUser().getUsername();
        // 用户可能没有头像，使用默认值
        this.userAvatar = "/images/default-avatar.png"; // 使用默认头像
        this.productId = review.getProduct().getId();
        this.content = review.getContent();
        this.rating = review.getRating();
        // 确保imageUrls不为null
        this.imageUrls = review.getImageUrls() != null ? review.getImageUrls() : new ArrayList<>();
        // 确保有效路径前缀
        if (this.imageUrls != null && !this.imageUrls.isEmpty()) {
            for (int i = 0; i < this.imageUrls.size(); i++) {
                String url = this.imageUrls.get(i);
                if (url != null && !url.startsWith("http") && !url.startsWith("/")) {
                    this.imageUrls.set(i, "/" + url); // 添加前缀斜杠
                }
            }
        }
        this.likesCount = review.getLikesCount();
        this.isLiked = false; // 默认为未点赞
        this.createTime = review.getCreateTime();

        // 获取订单号
        this.orderNo = review.getOrderNo();

        // 获取评论状态
        this.status = review.getStatus();

        // 如果有回复，转换为DTO
        if (review.getReplies() != null && !review.getReplies().isEmpty()) {
            this.replies = review.getReplies().stream()
                    .map(ReviewReplyDTO::new)
                    .collect(Collectors.toList());
        }
    }
}